<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: AesShared</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">AesShared</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../files/lib/ruby-aes/aes_shared_rb.html">
                lib/ruby-aes/aes_shared.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000002">decrypt_blocks</a>&nbsp;&nbsp;
      <a href="#M000004">decrypt_buffer</a>&nbsp;&nbsp;
      <a href="#M000001">encrypt_blocks</a>&nbsp;&nbsp;
      <a href="#M000003">encrypt_buffer</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000002" class="method-detail">
        <a name="M000002"></a>

        <div class="method-heading">
          <a href="#M000002" class="method-signature">
          <span class="method-name">decrypt_blocks</span><span class="method-args">(buffer)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000002-source">
<pre>
    <span class="ruby-comment cmt"># File lib/ruby-aes/aes_shared.rb, line 23</span>
23:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">decrypt_blocks</span>(<span class="ruby-identifier">buffer</span>)
24:         <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Bad block length&quot;</span> <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">buffer</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">%</span> <span class="ruby-value">16</span>).<span class="ruby-identifier">zero?</span>
25:         <span class="ruby-identifier">pt</span> = <span class="ruby-value str">&quot;&quot;</span>
26:         <span class="ruby-identifier">block</span> = <span class="ruby-value str">&quot;&quot;</span>
27:         <span class="ruby-identifier">buffer</span>.<span class="ruby-identifier">each_byte</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">char</span><span class="ruby-operator">|</span>
28:             <span class="ruby-identifier">block</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">char</span>
29:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">16</span>
30:                 <span class="ruby-identifier">pt</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">decrypt_block</span>(<span class="ruby-identifier">block</span>)
31:                 <span class="ruby-identifier">block</span> = <span class="ruby-value str">&quot;&quot;</span>
32:             <span class="ruby-keyword kw">end</span>
33:         <span class="ruby-keyword kw">end</span>
34:         <span class="ruby-identifier">pt</span>
35:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000004" class="method-detail">
        <a name="M000004"></a>

        <div class="method-heading">
          <a href="#M000004" class="method-signature">
          <span class="method-name">decrypt_buffer</span><span class="method-args">(buffer)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000004-source">
<pre>
    <span class="ruby-comment cmt"># File lib/ruby-aes/aes_shared.rb, line 55</span>
55:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">decrypt_buffer</span>(<span class="ruby-identifier">buffer</span>)
56:         <span class="ruby-identifier">pt</span> = <span class="ruby-value str">&quot;&quot;</span>
57:         <span class="ruby-identifier">block</span> = <span class="ruby-value str">&quot;&quot;</span>
58:         <span class="ruby-identifier">buffer</span>.<span class="ruby-identifier">each_byte</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">char</span><span class="ruby-operator">|</span>
59:             <span class="ruby-identifier">block</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">char</span>
60:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">16</span>
61:                 <span class="ruby-identifier">pt</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">decrypt_block</span>(<span class="ruby-identifier">block</span>)
62:                 <span class="ruby-identifier">block</span> = <span class="ruby-value str">&quot;&quot;</span>
63:             <span class="ruby-keyword kw">end</span>
64:         <span class="ruby-keyword kw">end</span>
65:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">!=</span> <span class="ruby-value">1</span>
66:             <span class="ruby-identifier">raise</span> <span class="ruby-value str">'Bad Block Padding'</span>
67:         <span class="ruby-keyword kw">elsif</span> (<span class="ruby-identifier">c</span> = <span class="ruby-identifier">block</span>[<span class="ruby-value">-1</span>]).<span class="ruby-identifier">zero?</span>
68:             <span class="ruby-identifier">pt</span>
69:         <span class="ruby-keyword kw">else</span>
70:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block</span> <span class="ruby-operator">*</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">pt</span>[<span class="ruby-operator">-</span><span class="ruby-identifier">c</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>]
71:                 <span class="ruby-identifier">pt</span>[<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-operator">-</span><span class="ruby-identifier">c</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>]
72:             <span class="ruby-keyword kw">else</span>
73:                 <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Bad Block Padding&quot;</span>
74:             <span class="ruby-keyword kw">end</span>
75:         <span class="ruby-keyword kw">end</span>
76:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000001" class="method-detail">
        <a name="M000001"></a>

        <div class="method-heading">
          <a href="#M000001" class="method-signature">
          <span class="method-name">encrypt_blocks</span><span class="method-args">(buffer)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000001-source">
<pre>
    <span class="ruby-comment cmt"># File lib/ruby-aes/aes_shared.rb, line 9</span>
 9:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">encrypt_blocks</span>(<span class="ruby-identifier">buffer</span>)
10:         <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Bad block length&quot;</span> <span class="ruby-keyword kw">unless</span> (<span class="ruby-identifier">buffer</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">%</span> <span class="ruby-value">16</span>).<span class="ruby-identifier">zero?</span>
11:         <span class="ruby-identifier">ct</span> = <span class="ruby-value str">&quot;&quot;</span>
12:         <span class="ruby-identifier">block</span> = <span class="ruby-value str">&quot;&quot;</span>
13:         <span class="ruby-identifier">buffer</span>.<span class="ruby-identifier">each_byte</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">char</span><span class="ruby-operator">|</span>
14:             <span class="ruby-identifier">block</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">char</span>
15:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">16</span>
16:                 <span class="ruby-identifier">ct</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">encrypt_block</span>(<span class="ruby-identifier">block</span>)
17:                 <span class="ruby-identifier">block</span> = <span class="ruby-value str">&quot;&quot;</span>
18:             <span class="ruby-keyword kw">end</span>
19:         <span class="ruby-keyword kw">end</span>
20:         <span class="ruby-identifier">ct</span>
21:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000003" class="method-detail">
        <a name="M000003"></a>

        <div class="method-heading">
          <a href="#M000003" class="method-signature">
          <span class="method-name">encrypt_buffer</span><span class="method-args">(buffer)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000003-source">
<pre>
    <span class="ruby-comment cmt"># File lib/ruby-aes/aes_shared.rb, line 37</span>
37:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">encrypt_buffer</span>(<span class="ruby-identifier">buffer</span>)
38:         <span class="ruby-identifier">ct</span> = <span class="ruby-value str">&quot;&quot;</span>
39:         <span class="ruby-identifier">block</span> = <span class="ruby-value str">&quot;&quot;</span>
40:         <span class="ruby-identifier">buffer</span>.<span class="ruby-identifier">each_byte</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">char</span><span class="ruby-operator">|</span>
41:             <span class="ruby-identifier">block</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">char</span>
42:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">16</span>
43:                 <span class="ruby-identifier">ct</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">encrypt_block</span>(<span class="ruby-identifier">block</span>)
44:                 <span class="ruby-identifier">block</span> = <span class="ruby-value str">&quot;&quot;</span>
45:             <span class="ruby-keyword kw">end</span>
46:         <span class="ruby-keyword kw">end</span>
47:         <span class="ruby-identifier">c</span> = <span class="ruby-value str">&quot;\000&quot;</span>
48:         <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">m</span> = <span class="ruby-value">16</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">block</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">%</span> <span class="ruby-value">16</span>) <span class="ruby-operator">!=</span> <span class="ruby-value">16</span>
49:             <span class="ruby-identifier">c</span> = <span class="ruby-identifier">m</span>.<span class="ruby-identifier">chr</span>
50:             <span class="ruby-identifier">ct</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">encrypt_block</span>(<span class="ruby-identifier">block</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">*</span> <span class="ruby-identifier">m</span>)
51:         <span class="ruby-keyword kw">end</span>
52:         <span class="ruby-identifier">ct</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">c</span>
53:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>